

package com.timestar.floating.utils;

import android.animation.Animator;
import android.annotation.SuppressLint;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateInterpolator;

import androidx.annotation.NonNull;

import com.timestar.floating.listener.ITouchButton;
import com.timestar.floating.listener.OnSwipeListener;


/**
 * 悬浮按钮的滑动事件拦截处理、Align、FloatingButton、Button
 */
public class TouchButtonHelper implements View.OnTouchListener {

    private static final long CLICK_VIEW_TIME = 100;
    private final String TAG = TouchButtonHelper.class.getSimpleName();

    private View mObservedView;
    private boolean mListenForTouchEvents;
    private float mDownX;
    private float mDownY;
    private int mPointerId;
    private ITouchButton mTouchLayout;
    private OnSwipeListener mOnStarSwipeListener;
    private long mDownTime;

    public void setOnSwipeListener(OnSwipeListener listener) {
        this.mOnStarSwipeListener = listener;
    }

    public TouchButtonHelper(ITouchButton layout) {
        mTouchLayout = layout;
    }


    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mDownTime = System.currentTimeMillis();
                if (!mListenForTouchEvents
                        || mTouchLayout == null
                        || !mTouchLayout.isLayoutEnabled()) {
                    return false;
                }
                v.getParent().requestDisallowInterceptTouchEvent(true);
                mPointerId = event.getPointerId(0);
                mDownX = event.getX(mPointerId);
                mDownY = event.getY(mPointerId);
                Log.e(TAG, "onTouch:ACTION_DOWN mDownX:" + mDownX + ",mDownX:" + mDownY);
                return true;
            case MotionEvent.ACTION_MOVE:
                int pointerIndex = event.findPointerIndex(mPointerId);
                if (pointerIndex < 0) return false;

                float dx = event.getX(pointerIndex) - mDownX;
                float dy = event.getY(pointerIndex) - mDownY;
                Log.e(TAG, "onTouch: mDownX:" + mDownX + ",mDownX:" + mDownY);
                Log.e(TAG, "onTouch: dx:" + dx + ",dy:" + dy);
                //dx:1434.0,dy:1458.5271
                onMoveObservedView(dx, dy);
                return true;

            case MotionEvent.ACTION_UP:
                v.getParent().requestDisallowInterceptTouchEvent(false);
                long touchTime = System.currentTimeMillis() - mDownTime;
                Log.e(TAG, "onTouch: touchTime:" + touchTime);
                if (touchTime > CLICK_VIEW_TIME) {
                    autoAlign();
                    return true;
                } else {
                    if (null != mOnStarSwipeListener) {
                        mOnStarSwipeListener.onClickDragItemListener(mObservedView);
                    }
                    return mObservedView.onTouchEvent(event);
                }
        }

        return false;
    }

    private void onMoveObservedView(float dx, float dy) {
        if (mObservedView == null) {
            return;
        }
        float newX = mObservedView.getX() + dx;
        float newY = mObservedView.getY() + dy;
        animateView(newX, newY);
    }

    public void registerObservedView(View view) {
        Log.e(TAG, "registerObservedView: ");
        if (view == null) return;
        mObservedView = view;
        mObservedView.setOnTouchListener(this);
        mListenForTouchEvents = true;
    }

    private void animateView(float x, float y) {
        mObservedView.animate()
                .setInterpolator(new AccelerateInterpolator())
                .x(x)
                .y(y)
                .alpha(1f)
                .setDuration(0)
                .setListener(new AnimUtils.AnimationEndListener() {
                    @Override
                    public void onAnimationEnd(@NonNull Animator animation) {

                    }
                }).start();

    }

    /**
     * 自动贴边
     */
    private void autoAlign() {
        //如果当前的坐标位置小于中心点位置，则贴坐标，否则贴右边。
        float x = mObservedView.getX() + mObservedView.getWidth() / 2F;
        float y = mObservedView.getY();

        if (y >= mTouchLayout.borderRectF().bottom - mObservedView.getHeight() / 2F) {
            y = mTouchLayout.borderRectF().bottom - mObservedView.getHeight() / 2F;
        }

        if (y < mTouchLayout.borderRectF().top + mObservedView.getHeight() * 2) {
            y = mTouchLayout.borderRectF().top + mObservedView.getHeight() * 2;
        }
        float maxX = mTouchLayout.borderRectF().right - mObservedView.getWidth();
        if (x < mTouchLayout.borderRectF().centerX()) {
            animateView(0, y);
        } else {
            animateView(maxX, y);
        }
    }

}
